home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / wtek0693.zip / OOPALLEY.ZIP / DICT.CPP < prev    next >
C/C++ Source or Header  |  1993-04-27  |  4KB  |  144 lines

  1. #include "dict.h"
  2. #include "lookupke.h"
  3. #include "assoc.h"
  4.  
  5. #define THIS    Dictionary
  6. #define BASE    Set
  7. DEFINE_CLASS(Dictionary,Set);
  8.  
  9. Dictionary::Dictionary(unsigned size) : Set(size) {}
  10.  
  11. Dictionary::Dictionary(const Dictionary& d) : Set(d) {}
  12.  
  13. void Dictionary::operator=(const Dictionary& d)
  14. {
  15.     this->Set::operator=(d);
  16. }
  17.  
  18. bool Dictionary::operator==(const Dictionary& d) const
  19. {
  20.     if (size() != d.size()) return NO;
  21.     DO(*this,LookupKey*,a)
  22.         if (!d.includesAssoc(*a)) return NO;
  23.     DONE
  24.     return YES;
  25. }
  26.  
  27. Object* Dictionary::add(const Object& ob)
  28. {
  29.     assertArgClass(ob,class_LookupKey,"add");
  30.     return Set::add(ob);
  31. }
  32.  
  33. Assoc* Dictionary::addAssoc(const Object& key, const Object& value)
  34. {
  35.     Assoc* a = new Assoc(key,value);
  36.     Assoc* b = (Assoc*)Set::add(*a);
  37.     if (a != b) {
  38.         delete a;
  39.         //DTerror("OOPS_DUPKEY,DEFAULT,this,className(),\"addAssoc\",key.className(),&key",className());
  40.         DTerror("Tried to add duplicate key: ",className());
  41.     }
  42.     return b;
  43. }
  44.  
  45. Collection& Dictionary::addContentsTo(Collection& cltn) const
  46. {
  47.     DO(*this,LookupKey*,a) cltn.add(*(a->value())); DONE
  48.     return cltn;
  49. }
  50.  
  51. Object* Dictionary::remove(const Object& ob)
  52. {
  53.     assertArgClass(ob,class_LookupKey,"remove");
  54.     return Set::remove(ob);
  55. }
  56.  
  57. #pragma warn -rvl   // Turn of Warning: Function should return a value
  58. Object* Dictionary::atKey(const Object& key) const
  59. {
  60.     register Object* p = findObjectWithKey(key);
  61.     if (p==nil)
  62.     {
  63.         //DTerror("OOPS_KEYNOTFOUND,DEFAULT,this,className(),key.className(),&key",className());
  64.         DTerror("Key not found: ",className());     // aborts, return not executed
  65.         // return (Object*)NULL;                       // avoids Warning message
  66.     } else return ((LookupKey*)p)->value();
  67. }
  68. #pragma warn .rvl
  69.  
  70. #pragma warn -rvl   // Turn of Warning: Function should return a value
  71. Object* Dictionary::atKey(const Object& key, const Object& newValue)
  72. {
  73.     register Object* p = findObjectWithKey(key);
  74.     if (p==nil)
  75.     {
  76.         //DTerror("OOPS_KEYNOTFOUND,DEFAULT,this,className(),key.className(),&key",className());
  77.         DTerror("Key not found: ",className());     // aborts, return not executed
  78.         // return (Object*)NULL;                       // avoids Warning message
  79.     } else return ((LookupKey*)p)->value(newValue);
  80. }
  81. #pragma warn .rvl
  82.  
  83. LookupKey& Dictionary::assocAt(const Object& key) const
  84. {
  85.     return *(LookupKey*)findObjectWithKey(key);
  86. }
  87.  
  88. Collection& Dictionary::addKeysTo(Collection& cltn) const
  89. {
  90.     DO(*this,LookupKey*,a) cltn.add(*a->key()); DONE
  91.     return cltn;
  92. }
  93.  
  94. Collection& Dictionary::addValuesTo(Collection& cltn) const
  95. {
  96.     return addContentsTo(cltn);
  97. }
  98.  
  99. Object* Dictionary::keyAtValue(const Object& val) const
  100. {
  101.     DO(*this,LookupKey*,a)
  102.         if (val.isEqual(*a->value())) return a->key();
  103.     DONE
  104.     return (Object*)nil;
  105. }
  106.  
  107. unsigned Dictionary::occurrencesOf(const Object& val) const
  108. {
  109.     register unsigned n =0;
  110.     DO(*this,LookupKey*,a) if (val.isEqual(*a->value())) n++; DONE
  111.     return n;
  112. }
  113.  
  114. bool Dictionary::includesAssoc(const LookupKey& asc) const
  115. {
  116.     register Object* p = findObjectWithKey(asc);
  117.     if (p==nil) return NO;
  118.     return (asc.value())->isEqual(*((LookupKey*)p)->value());
  119. }
  120.  
  121. bool Dictionary::includesKey(const Object& key) const
  122. {
  123.     if (findObjectWithKey(key) == nil) return NO;
  124.     else return YES;
  125. }
  126.  
  127. bool Dictionary::isEqual(const Object& ob) const
  128. {
  129.     return ob.isSpecies(class_Dictionary) && *this==*(Dictionary*)&ob;
  130. }
  131.  
  132. const Class* Dictionary::species() const { return &class_Dictionary; }
  133.  
  134. LookupKey& Dictionary::removeAssoc(const LookupKey& asc)
  135. {
  136.     return *(LookupKey*)remove(asc);
  137. }
  138.  
  139. LookupKey& Dictionary::removeKey(const Object& key)
  140. {
  141.     return removeAssoc(assocAt(key));
  142. }
  143.  
  144.